package cn.edu.nuc.onlinestore.frame;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.edu.nuc.onlinestore.model.Goods;
import demo.factor.Ifactor.IFactory;

public class AdminStore extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JFrame store;
	private JFrame adminlogin;
	private String str;

	
	public JFrame getAdminlogin() {
		return adminlogin;
	}


	public void setAdminlogin(JFrame adminlogin) {
		this.adminlogin = adminlogin;
	}


	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminStore frame = new AdminStore();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/
	/**
	 * Create the frame.
	 */
	public AdminStore(/*JFrame frame*/) {
		
		this.store=this;
		//关闭login窗口
		//frame.setVisible(false);
		setTitle("中北商场后台管理系统--当前用户"+AdminName.name);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		//model.addColumn("操作");
		
		/*model.addRow(new String[]{"1","水杯","15.00","200"});
		model.addRow(new String[]{"2","水瓶","35.00","200"});
		model.addRow(new String[]{"3","天堂伞","55.00","200"});
		model.addRow(new String[]{"4","男袜","8.00","200"});
		model.addRow(new String[]{"5","农夫山泉","2.00","200"});
		model.addRow(new String[]{"6","毛巾","9.90","200"});
		model.addRow(new String[]{"7","牙刷","15.00","200"});
		model.addRow(new String[]{"8","洗发水","15.00","200"});
		model.addRow(new String[]{"9","牙膏","15.00","200"});
		model.addRow(new String[]{"10","海尔全自动洗衣机","2,699.00","200"});*/
		
		
		
		/*
		//得到所有商品列表
		Set<Goods> set=Util.SeverSelectAllGoods();
		System.out.println(set);
		Iterator<Goods> it=set.iterator();
		Goods g;
		int i=0;
		while(it.hasNext())
		{
			g=it.next();
			//i=i+1;
			model.addRow(new String[]{""+g.getGoodsId(),g.getName(),""+g.getPeice(),""+g.getNumber()});
		}*/
		
		
		JTable table = new JTable( model );

		JScrollPane pane = new JScrollPane( table );
		
		//得到所有商品列表
		showGoods(str,table,model);
		
		panel.add(pane);
		contentPane.add(panel);
		
		JButton button = new JButton("添加商品");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminAdd add = new AdminAdd(/*store*/);
				add.setAdminlogin(adminlogin);
				add.setStore(store);
				
				
				add.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				add.setVisible(true);
			}
		});
		button.setBounds(386, 45, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("修改商品");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到当前选中商品项的行数
				int row=table.getSelectedRow();
				//判断行数的值
				if(row==-1)
				{
					JOptionPane.showConfirmDialog(null, "没有选择一行", "",JOptionPane.YES_NO_OPTION);
				}else{
					//得到行数
					int id=Integer.parseInt(model.getValueAt(row, 0).toString());
					//修改页面
					AdminUpdate u = new AdminUpdate(id);
					u.setAdminlogin(adminlogin);
					u.setStore(store);
					u.setModel(model);
					u.setTable(table);
					
					u.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					u.setVisible(true);
				}
			}
		});
		button_1.setBounds(489, 45, 93, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("删除选中商品");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到当前选中商品项
				int row=table.getSelectedRow();
				//判断是否选中行数
				if(row==-1)
				{
					JOptionPane.showConfirmDialog(null, "没有选择一行", "",JOptionPane.YES_NO_OPTION);
				}else{
					//得到good id
					int id=Integer.parseInt(model.getValueAt(row, 0).toString());
					//System.out.println("idididdidiiddidiidididiidididi"+model.getValueAt(row, 0).toString());
					//删除这一行
					int a=JOptionPane.showConfirmDialog(null, "确定要删除吗", "选择一个选项",JOptionPane.YES_NO_OPTION);
					if(a==JOptionPane.YES_OPTION){
						//执行删除
						IFactory.deleteGoods(id);
						//显示
						model.removeRow(row);
					}
				}
			}
		});
		button_2.setBounds(587, 45, 119, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				adminlogin.setVisible(true);
				setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				setVisible(false);
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);
		
		JLabel label = new JLabel("当前在线用户数:");
		label.setBounds(10, 10, 162, 15);
		contentPane.add(label);
		
		JLabel lblid = new JLabel("商品编号:");
		lblid.setBounds(10, 53, 54, 15);
		contentPane.add(lblid);
		
		textField = new JTextField();
		textField.setBounds(68, 50, 104, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str=textField.getText();
				showGoods(str,table,model);
			}
		});
		button_4.setBounds(180, 49, 93, 23);
		contentPane.add(button_4);
		
		
	}
	
	
	//登录页面开始的查询操作函数
	public Set<Goods> showGoods(String str,JTable table,DefaultTableModel model)
	{
		Set<Goods> set = null;
		Goods g=null;
		//先删除所有行
		for (int i =model.getRowCount()-1; i >=0 ;i--) 
		{
			   model.removeRow(i);
		}
		//如果字符串为空
		if("".equals(str)||null==str)
		{
			set=IFactory.selectAllGoods();
			Iterator<Goods> goods = set.iterator();
			while(goods.hasNext())
			{
				g=goods.next();
				model.addRow(new String[]{""+g.getGoodsId(),g.getName(),""+g.getPeice(),""+g.getNumber()});
			}
		}else{
			
			//查找再添加
			try{
				g=IFactory.selectAGoods(Integer.parseInt(str));
				
				if(g!=null)
				{
					model.addRow(new String[]{""+g.getGoodsId(),g.getName(),""+g.getPeice(),""+g.getNumber()});
				}	
			}catch(Exception e){
				
				//这里根据名字查询
				
			}
			
			table.setModel(model);
			
		}
		return set;
	}
	
	
}
